package com.example.pushdeom.UIActivity;

import android.content.Intent;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;

import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.ImageLoader;
import com.android.volley.toolbox.ImageRequest;
import com.android.volley.toolbox.NetworkImageView;
import com.android.volley.toolbox.Volley;
import com.bumptech.glide.Glide;
import com.bumptech.glide.load.resource.bitmap.CircleCrop;
import com.bumptech.glide.request.RequestOptions;
import com.example.pushdeom.Bean.AppSharedPreferences;
import com.example.pushdeom.Bean.ShareList;
import com.example.pushdeom.Bean.ShareUiListener;
import com.example.pushdeom.PushGetTokenActivity;
import com.example.pushdeom.R;
import com.tencent.connect.common.Constants;
import com.tencent.tauth.Tencent;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class MainActivity extends AppCompatActivity {

    @BindView(R.id.btn_qqshare)
    Button btnQqshare;
    @BindView(R.id.btn_qzoneshare)
    Button btnQzoneshare;
    @BindView(R.id.btn_huaweipush)
    Button btnHuaweipush;
    @BindView(R.id.btn_qqlogin)
    Button btnQqlogin;
    @BindView(R.id.tv_userid)
    TextView tvUserid;
    @BindView(R.id.img_user)
    ImageView imgUser;
    @BindView(R.id.btn_qqcancle)
    Button btnQqcancle;
    @BindView(R.id.btn_qqok)
    Button btnQqok;
    @BindView(R.id.tv_usergender)
    TextView tvUsergender;
    @BindView(R.id.img_1)
    ImageView img1;
    @BindView(R.id.networkImageView)
    NetworkImageView networkImageView;
    private String url;
    private String userName, userImg, userGender;
    private Tencent mTencent;
    RequestQueue mQueue;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        ButterKnife.bind(this);
        if (mTencent == null) {
            mTencent = Tencent.createInstance("101919484", this.getApplicationContext());
        }
        initView();
        mQueue = Volley.newRequestQueue(this);
    }

    @OnClick({R.id.btn_qqshare, R.id.btn_qzoneshare, R.id.btn_huaweipush, R.id.btn_qqlogin, R.id.img_user, R.id.tv_userid, R.id.btn_qqcancle, R.id.btn_qqok})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.btn_qqshare:
                ShareList.qqShare(mTencent, MainActivity.this);
                break;
            case R.id.btn_qzoneshare:
                ShareList.qqQzoneShare(mTencent, MainActivity.this);
                break;
            case R.id.btn_huaweipush:
                Intent intent = new Intent(MainActivity.this, PushGetTokenActivity.class);
                startActivity(intent);
                break;
            case R.id.btn_qqlogin:
                ImageRequest imageRequest = new ImageRequest("https://ss1.bdstatic.com/70cFuXSh_Q1YnxGkpoWK1HF6hhy/it/u=1194131577,2954769920&fm=26&gp=0.jpg", new Response.Listener<Bitmap>() {
                    @Override
                    public void onResponse(Bitmap response) {
                        //成功了
                        img1.setImageBitmap(response);
                    }
                },100,100, Bitmap.Config.RGB_565, new Response.ErrorListener(){
                    @Override
                    public void onErrorResponse(VolleyError error) {
                        //失败后
                        img1.setImageResource(R.mipmap.ic_launcher);
                    }
                });
                mQueue.add(imageRequest);
                //使用volley里面networkImageView控件加载网络图片
                ImageLoader imageLoader = new ImageLoader(mQueue, new ImageLoader.ImageCache() {
                    @Nullable
                    @Override
                    public Bitmap getBitmap(String url) {
                        return null;
                    }

                    @Override
                    public void putBitmap(String url, Bitmap bitmap) {

                    }
                });
                //等待图
                networkImageView.setDefaultImageResId(R.mipmap.ic_launcher);
                //加载失败图
                networkImageView.setErrorImageResId(R.mipmap.ic_launcher_round);
                //网络加载图
                networkImageView.setImageUrl("https://ss3.bdstatic.com/70cFv8Sh_Q1YnxGkpoWK1HF6hhy/it/u=3774219000,1098946849&fm=26&gp=0.jpg", imageLoader);
                break;
            case R.id.img_user:
                Toast.makeText(getApplicationContext(), "点击头像", Toast.LENGTH_LONG).show();
                break;
            case R.id.tv_userid:
                Toast.makeText(getApplicationContext(), "点击名称", Toast.LENGTH_LONG).show();
                break;
            case R.id.btn_qqcancle:
                logout();
                break;
            case R.id.btn_qqok:
                Glide.with(getApplicationContext()).load("https://ss1.bdstatic.com/70cFvXSh_Q1YnxGkpoWK1HF6hhy/it/u=2253864479,2844278604&fm=26&gp=0.jpg")
                        .apply(RequestOptions.bitmapTransform(new CircleCrop()))
                        .into(imgUser);
                break;
        }
    }

    //注销
    public void logout() {
        //这是QQ官方给的方法
        mTencent.logout(this);
        //清楚缓存中数据
        AppSharedPreferences.clearPreference(getApplicationContext());
        Intent intent_login = new Intent();
        intent_login.setClass(MainActivity.this, LoginActivity.class);
        intent_login.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); //关键的一句，将新的activity置为栈顶
        startActivity(intent_login);
        finish();
    }

    public void initView() {
        userName = AppSharedPreferences.getPreference(getApplicationContext(), "nickname", "");
        userGender = AppSharedPreferences.getPreference(getApplicationContext(), "gender", "无");
        userImg = AppSharedPreferences.getPreference(getApplicationContext(), "userImg", "");
        Glide.with(getApplicationContext()).load(userImg)
                .apply(RequestOptions.bitmapTransform(new CircleCrop()))
                .into(imgUser);
        tvUserid.setText(userName);
        tvUsergender.setText(userGender);
    }


    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        Tencent.onActivityResultData(requestCode, resultCode, data, new ShareUiListener());
        if (requestCode == Constants.REQUEST_API) {
            if (resultCode == Constants.REQUEST_QQ_SHARE || resultCode == Constants.REQUEST_QZONE_SHARE || resultCode == Constants.REQUEST_OLD_SHARE) {
                Tencent.handleResultData(data, new ShareUiListener());
            }
        }
    }
}


